package org.idea.spring.aop.version2.proxy;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.idea.spring.aop.version2.interceptor.IMethodInterceptor;
import org.idea.spring.aop.version2.interceptor.IMethodInterceptorChains;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * @Author linhao
 * @Date created in 11:03 上午 2021/5/15
 */
public class CglibTargetInterceptor implements MethodInterceptor{

    public  IMethodInterceptorChains iMethodInterceptorChains;

    public CglibTargetInterceptor(IMethodInterceptorChains iMethodInterceptorChains) {
        this.iMethodInterceptorChains = iMethodInterceptorChains;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        //before执行是没有返回结果的
        IMethodInterceptor beforeIMethodInterceptor = iMethodInterceptorChains.getMethodInterceptor("before");
        beforeIMethodInterceptor.intercept(objects);
        Object result = methodProxy.invokeSuper(o,objects);
        //after执行也是没有返回结果的
        IMethodInterceptor afterIMethodInterceptor = iMethodInterceptorChains.getMethodInterceptor("after");
        afterIMethodInterceptor.intercept(objects);
        return result;
    }

}
